<?php
namespace app\Service;

class Algorithm {
    /**
     * 计算买入卖出
     * @param $coinConf 币种设置
     * @return array [code, price, reason] code:0不操作，1卖出， 2买入; price:交易价格
     */
    public static function compute($coinConf)
    {
        //账户状态，暂停则不交易
        if ($coinConf['status'] != '1')
            return [0];
        //操作状态，正在交易中则不交易
        if ($coinConf['op_status'] == '1') {
            if ((time() - $coinConf['op_time']) > 30) {
                //超时重置状态
                static::resetOpStatus($coinConf, '操作超时');
            }
            return [0];
        }
        $lastPrice = $coinConf['last_price'];
        //获取上次交易之后的价格信息
        $priceInfo = static::getPriceInfo($coinConf);
        $minPrice = $priceInfo[0];
        $maxPrice = $priceInfo[1];
        $nowPrice = $priceInfo[2];
        //上涨卖出条件判断
        if (/*$minPrice >= $lastPrice*/ //上涨
            /*&&*/ ($maxPrice - $lastPrice) / $lastPrice >= $coinConf['param1'] / 100 //上涨进入
            && ($maxPrice - $nowPrice) / $maxPrice >= $coinConf['param2'] / 100) { //上涨卖出
            return [1, $nowPrice, '上涨卖出'];
        }
        //下跌买入条件判断
        if (($lastPrice - $minPrice) / $lastPrice >= $coinConf['param3'] / 100 //下跌进入
            && ($nowPrice - $minPrice) / $minPrice >= $coinConf['param4'] / 100) { //下跌买入
            return [2, $nowPrice, '下跌买入'];
        }
        //震荡卖出条件判断
        if (($maxPrice - $lastPrice) / $lastPrice >= $coinConf['param5'] / 100 //震荡上涨进入
            && ($maxPrice - $nowPrice) / $maxPrice >= $coinConf['param6'] / 100) { //震荡上涨卖出
            return [1, $nowPrice, '震荡上涨卖出'];
        }
        //震荡买入条件判断
        if (($lastPrice - $minPrice) / $lastPrice >= $coinConf['param7'] / 100 //震荡下跌进入
            && ($nowPrice - $minPrice) / $minPrice >= $coinConf['param8'] / 100) { //震荡下跌买入
            return [2, $nowPrice, '震荡下跌买入'];
        }
        //不操作
        return [0];
    }

    /**
     * 重置交易状态
     * @param $coinConf
     * @param $reason
     * @return bool
     */
    private static function resetOpStatus($coinConf, $reason)
    {
        //修改账户操作状态
        //修改交易状态
        return true;
    }

    /**
     * 获取价格信息
     * @param $coinConf
     * @return array
     */
    private static function getPriceInfo($coinConf)
    {
        $lastTradePrice = $coinConf['last_trade_price'];
        $lastTradeTime = $coinConf['last_trade_time'];
        $coinName = $coinConf['coin_name'];
        $nowPrice = 0;
        $sql = "select price as nowPrice
                from price where coin_name='{$coinName}' and created_at >= '{$lastTradeTime}' order by id desc limit 1";
        $result = DB::select($sql);
        if (!empty($result) && count($result)) {
            foreach ($result as $row) {
                $nowPrice = $row->nowPrice;
            }
        }
        $minPrice = $maxPrice = 0;
        $sql = "select max(price) as maxPrice, min(price) as minPrice
                from price where coin_name='{$coinName}' and created_at >= '{$lastTradeTime}'";
        $result = DB::select($sql);
        if (!empty($result) && count($result)) {
            foreach ($result as $row) {
                $minPrice = $row->minPrice;
                $maxPrice = $row->maxPrice;
            }
        }
        return [$minPrice, $maxPrice, $nowPrice];
    }
}